-- seen.lua
-- Monitors what the each person said last and when they said it
-- Version 1.0
-- By Tyrone

---------------------------------------

--[[
Changelog:
1.0
-Tracks user messages and time sent

]]



-- set up all command prefixes
cmdNames = {
    "seen"
}


COOLDOWN = 60 -- plugin cooldown, 0 for none
_,bot_name = VH:GetConfig("config", "hub_security")
bot_desc = "bot description here"
bot_email = "bot@nowhere.com"
use_bot = false

function doCommand(nick, data, z)
	
	if (data == nil) then
		if (seen[nick] == nil) then
			SendMessageToUser("You haven't said anything yet!", nick, bot_name)
		else
			SendMessageToUser("I remember you saying: "..seen[nick].." at "..time[nick]..".", nick, bot_name)
		end
	elseif (seen[data] == nil) then
		SendMessageToUser("No user by the name of "..data.." has ever spoken since the last hub restart.", nick, bot_name)
	else
		SendMessageToUser("I remember this user saying: "..seen[data].." at "..time[data]..".", nick, bot_name)
	end
	
end

function doHelp(nick)
    SendMessageToUser("Usage: ", nick, bot_name)
    DisplayHelpMessage("help", "Shows this message.", nick)
    -- put coad in here for help commands:
	DisplayHelpMessage("", "Shows you the last thing you said", nick)
	DisplayHelpMessage("user", "Shows you the last thing user said", nick)
end

-- insert other functions here
seen = {}
time = {}
---------------------------------------
-- you can ignore below here
timeLeft = {}

function Main()
    -- Check if script should add a new bot.
    if (use_bot) then
    -- Add new bot.
        VH:AddRobot(bot_name, 10, bot_desc, "Bot ", bot_email, "0")
    end
end

function UnLoad()
	if (use_bot) then
    	VH:DelRobot(bot_name)
	end
end

function VH_OnParsedMsgChat(nick, data)
	if (string.find(data, "^%."..cmdNames[1])) then
		_,_,cmd = string.find(data, "^%."..cmdNames[1].."%s(.+)")
		if (cmd == "help") then
		    doHelp(nick)
		else
        	doCommand(nick, cmd, cmdNames[1])
        end
    	return 0
    else
		seen[nick] = data
		time[nick] = os.date()
		return 1
	end
	return 1
end

function ready(nick)
    if (timeLeft[nick] == 0) then
		timeLeft[nick] = COOLDOWN
	elseif (timeLeft[nick] == nil) then
		timeLeft[nick] = COOLDOWN
	else
		SendMessageToUser("This command can only be used every "..COOLDOWN.." seconds.  You have "..timeLeft[nick].." seconds to wait.", nick, bot_name)
		return true
	end
	return false
end

function SendMessageToHub(data, from)
	return VH:SendDataToAll("<"..from.."> "..data.."|",0,10)
end

function SendMessageToUser(data, nick, from)
	return VH:SendDataToUser("<"..from.."> "..data.."|", nick)
end

function SendPrivateMessageToUser(user, data, from)
    return VH:SendDataToUser("$To: "..user.." From: "..from.." $<"..from.."> "..data.."|", user);
end

function DisplayHelpMessage(cmd, msg, nick)
    SendMessageToUser("        ."..cmdNames[1].." "..cmd, nick, bot_name)
    SendMessageToUser("        "..msg, nick, bot_name)
end

function UserIsOnline(nick)
	result, sIP = VH:GetUserIP(nick)
	if (string.len(sIP) < 7) then
		return false
	else
		return true
	end
end

function UserIP(nick)
	result, sIP = VH:GetUserIP(nick)
	if (string.len(sIP) < 7) then
		return 0
	else
		return sIP
	end
end

function UserClass(nick)
	res, class = VH:GetUserClass(nick)
	if res and class then return class else return 0 end
end

function SendEmoteToHub(data, from)
	result, err = VH:SendDataToAll("<"..from.."> ".."/me "..data.."|",0,10)
	return 1
end

function VH_OnTimer()
	for nick,time in pairs(timeLeft) do
		if (time > 0) then
			timeLeft[nick] = timeLeft[nick] - 1
		end
	end
	return 1
end